home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / dialog.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  147 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1993, David Koblas (koblas@netcom.com)                  | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <X11/Intrinsic.h>
  16. #include <X11/Shell.h>
  17. #include <X11/Xaw/Command.h>
  18. #include <X11/Xaw/Toggle.h>
  19. #include <X11/Xaw/Form.h>
  20. #include <X11/Xaw/Label.h>
  21. #include <X11/StringDefs.h>
  22. #include <stdio.h>
  23. #ifdef MISSING_STDARG_H
  24. #include <varargs.h>
  25. #else
  26. #include <stdarg.h>
  27. #endif
  28. #include "misc.h"
  29.  
  30. /*
  31. **  One standard generic dialog alert box.
  32. */
  33. typedef struct {
  34.     XtCallbackProc    okFunc, cancelFunc;
  35.     void        *data;    
  36.     Widget        parent, shell;
  37. } arg_t;
  38.  
  39. static void commonCallback(XtCallbackProc f, arg_t *arg)
  40. {
  41.     void        *d = arg->data;
  42.     Widget        p  = arg->parent;
  43.  
  44.     XtDestroyWidget(arg->shell);
  45.     XtFree((XtPointer)arg);
  46.  
  47.     if (f != NULL)
  48.         f(p, d, NULL);
  49. }
  50. static void cancelCallback(Widget w, XtPointer argArg, XtPointer junk2)
  51. {
  52.     arg_t    *arg = (arg_t *)argArg;
  53.  
  54.     commonCallback(arg->cancelFunc, arg);
  55. }
  56. static void okCallback(Widget w, XtPointer argArg, XtPointer junk2)
  57. {
  58.     arg_t    *arg = (arg_t *)argArg;
  59.  
  60.     commonCallback(arg->okFunc, arg);
  61. }
  62.  
  63. void AlertBox(Widget parent, char *msg, XtCallbackProc okProc, XtCallbackProc nokProc, void *data)
  64. {
  65.     Position    x, y;
  66.     Widget        shell;
  67.     Widget        form, title, okButton = None, cancelButton = None;
  68.     arg_t        *arg = XtNew(arg_t);
  69.     
  70.         XtVaGetValues(GetShell(parent), XtNx, &x, XtNy, &y, NULL);
  71.  
  72.         shell = XtVaCreatePopupShell("alert",
  73.                         transientShellWidgetClass, GetShell(parent),
  74.             XtNx, x,
  75.             XtNy, y,
  76.                         NULL);
  77.         form = XtVaCreateManagedWidget(NULL,
  78.                                 formWidgetClass, shell,
  79.                                 XtNborderWidth, 0,
  80.                                 NULL);
  81.  
  82.     title = XtVaCreateManagedWidget("title",
  83.                 labelWidgetClass, form,
  84.                 XtNlabel, msg, 
  85.                 XtNborderWidth, 0,
  86.                 NULL);
  87.  
  88.     arg->shell = shell;
  89.     arg->okFunc = okProc;
  90.     arg->cancelFunc = nokProc;
  91.     arg->parent = parent;
  92.     arg->data = data;
  93.  
  94.     okButton = XtVaCreateManagedWidget("ok",
  95.             commandWidgetClass, form,
  96.             XtNfromVert, title,
  97.             XtNlabel, "OK",
  98.             /* XtNaccelerators, accel, */
  99.             NULL);
  100.     XtAddCallback(okButton, XtNcallback, okCallback, (XtPointer)arg);
  101.  
  102.     if (nokProc != NULL) {
  103.         cancelButton = XtVaCreateManagedWidget("cancel",
  104.                 commandWidgetClass, form,
  105.                 XtNfromVert, title,
  106.                 XtNfromHoriz, okButton,
  107.                 XtNlabel, "Cancel",
  108.                 NULL);
  109.         XtAddCallback(cancelButton, XtNcallback, cancelCallback, (XtPointer)arg);
  110.     }
  111.  
  112.  
  113.     AddDestroyCallback(shell, (void (*)(Widget, void *, XEvent *))cancelCallback, NULL);
  114.     XtPopup(shell, XtGrabExclusive);
  115. }
  116.  
  117. #ifdef MISSING_STDARG_H
  118. void Notice(va_alist)
  119. va_dcl
  120. {
  121.     static char    msg[512];
  122.     va_list        ap;
  123.     char        *fmt;
  124.     Widget        w;
  125.  
  126.     va_start(ap);
  127.     w   = va_arg(ap, Widget);
  128.     fmt = va_arg(ap, char *);
  129.     vsprintf(msg, fmt, ap);
  130.  
  131.     AlertBox(GetShell(w), msg, NULL, NULL, NULL);
  132. }
  133. #else
  134. void Notice(Widget w, ...)
  135. {
  136.     static char    msg[512];
  137.     va_list        ap;
  138.     char        *fmt;
  139.  
  140.     va_start(ap, w);
  141.     fmt = va_arg(ap, char *);
  142.     vsprintf(msg, fmt, ap);
  143.  
  144.     AlertBox(GetShell(w), msg, NULL, NULL, NULL);
  145. }
  146. #endif
  147.